define(["require",
	'config/appregister',
	'business/blogpost/service/blogHttp',
	"jquery",
	"domReady",
	"business/home/directives/editorDirective",
	"business/home/services/filterService",
"business/services/detailNavComp"], function(require, app, httpServices) {
	app.controller('blogpostCtrl', function($scope, $http, $state, $stateParams) {
		$scope.blogTitle='';
		//取博文type
		if(!angular.isUndefined($stateParams.blogType)) {
			$scope.modtype = $stateParams.blogType;
		}		
		$scope.modtype = $stateParams.blogType;
		//关闭发布成功小弹出盒子
		$scope.closeok = function() {
			$("#isok").hide();
			if($scope.modtype==1){
				$state.go("comIndexCtrl.resource");
			}else{
				$state.go("bloglist");
			}
		};
		//回显
		var a1 = 1,
			a2 = 1,
			a3 = 1;
			
		$scope.data = {
			"title": "",
			"content": "",
			"labelIds": "",
			"tcId": "",
			"scId": "",
			"open": "",
			"dcId": "",
			"type": 0
		};
		//控制每个标签的长度
		$scope.controlLength = function(str){
			var l = str.length;
			var blen = 0;
			for(var i=0; i<l; i++) {
				if (str.charCodeAt(i) > 255) {
					blen ++;
				}
				blen ++;
			}
			if(blen == 32){
				var maxLength = $scope.tagsWork.length;
			}
			if(blen > 32){
				$scope.tagsWork.length = maxLength;
			}
		};

		$scope.tags2 = [//ngInputTab自己写方法

        ];
		$scope.saveTagsname=function(a){
			if(a == ''){
				return;
			}
			if($scope.tags2.length == 6){
				alert('最多只能添加六个标签哦，亲');
				return;
			}
			if($scope.tags2.indexOf(a) != -1){
				alert("关键词不能相同哦")
			}else{
				$scope.tags2.push(a);
				$scope.tagsWork = '';
			}
		};

		$scope.deleteInput=function($index){
            $scope.tags2.splice($index,1);
        };
	//选择分类
		//一级分类
		oneList();
		function oneList(){
			var req = {
				type:'GET',
				uri:'getTopCategory',
			};
			httpServices.wtfHttpService(req).success(function(data){
				if(!data)return;
				if(data.code==0) {
					$scope.$apply(function(){
						$scope.onelist = data.data;							
					})
				}else {
					alert(data.msg);
				}
			}).error(function(){
				alert("一级分类请求失败");
			});
		};


		$scope.getscid = function(scid, $index) {
			if($index == 0) {
				$(".h21").html("国家：");
				$(".h22").html("专业：");
				$(".h23").html("种类：");
				$(".h24").hide();
				$(".yiwai").hide();
			}else if($index == 1){
				$(".h21").html("专业：");
				$(".h22").html("内容：");
				$(".h23").html("项目模式：");	
				$(".h24").hide();
				$(".yiwai").hide();
			}else if($index == 2){
				$(".h24").show();
				$(".yiwai").show();
				$(".h21").html("经济环境：");
				$(".h22").html("法律法规：");
				$(".h23").html("合同管理：");	
				$(".h24").html("宗教文化：");	
			}
			$scope.data.scId = scid;
			var thebox = $(".dandan");
			var max = thebox[$index];
			var ss = $(max).next();
			var ele = $(".showbox");
			ele.hide();
			ss.show();
			var zhuanye = [];
			var leirong = [];
			var moshi = [];
			var zongj = [];
			
			var req = {
				type:'GET',
				uri:'getDetailCategory',
				data:{
					scId:scid
				}
			};
			httpServices.wtfHttpService(req).success(function(data){
				if(!data)return;
				if(data.code==0){
					$scope.$apply(function(){
						var four = data.data;
						for(i in four) {
							if(four[i].dcType == 1 || four[i].dcType == 4 || four[i].dcType == 7) {
								zhuanye.push(four[i]);
							} else if(four[i].dcType == 2 || four[i].dcType == 5 || four[i].dcType == 8) {				
								leirong.push(four[i]);
							} else if(four[i].dcType == 3 || four[i].dcType == 6 || four[i].dcType == 9) {
								moshi.push(four[i]);
							} else if(four[i].dcType == 10) {
								zongj.push(four[i]);
							}
						}
						$scope.zy = zhuanye;
						$scope.lr = leirong;
						$scope.ms = moshi;
						$scope.duo = zongj;
					});
				}else {
					alert(data.msg);
				}
			}).error(function(){
				alert("分类请求失败");
			});
		};

		//
		$scope.getthis = function($index, dcid) {
				var qunsed = $(".iask-three ul li");
				for(var i = 0; i < qunsed.length; i++) {
					qunsed[i].style.color = "#666";
				}
				qunsed[$index].style.color = "#30a3ff";
				$scope.data.dcId = dcid;
			}
		//	
		$scope.getthree = function(scid, $index) {
			$scope.scid = scid;
				var quns = $(".iask-pleaseed ul li");
				for(var i = 0; i < quns.length; i++) {
					quns[i].style.color = "#666";
				}
				quns[$index].style.color = "#30a3ff";
				$(".iask-three").show();
				var req = {
					type:'GET',
					uri:'getDetailCategory',
					data:{
						scId:$scope.scid
					}
				};
				httpServices.wtfHttpService(req).success(function(data){
					if(!data)return;
					if(data.code==0) {
						$scope.$apply(function(){
							$scope.three = data.data;
						});	
					}else {
						alert(data.msg);
					}
				}).error(function(){
					alert("渲染三级分类失败");
				});
				$scope.data.scId = scid;
		};
		//
		$scope.getsecond = function(tcid, $index) {
			$scope.tcid = tcid;
			var qun = $(".iask-please ul li");
			for(var i = 0; i < qun.length; i++) {
				qun[i].style.color = "#666";
			}
			qun[$index].style.color = "#30a3ff";
			if(tcid == 2) {
				$scope.data.dcId = undefined;
				var req = {
					type:'GET',
					uri:'getSecondaryCategory',
					data:{
						tcId:$scope.tcid
					}
				};
				httpServices.wtfHttpService(req).success(function(data){
					if(!data)return;
					if(data.code == 0) {
						$scope.$apply(function(){
							$(".iask-pleaseed").hide();
							$(".iask-three").hide();
							$(".iask-them").show();
							$scope.them = data.data;								
						});
					}else {
						alert(data.msg);
					}
				}).error(function(){
					alert("渲染国际工程里的list失败");
				});
			} else {
				$scope.radiosarr = [];
				$(".iask-them").hide();
				$(".iask-three").hide();
				$(".iask-pleaseed").show();
				
				var req = {
					type:'GET',
					uri:'getSecondaryCategory',
					data:{
						tcId:$scope.tcid
					}
				};
				httpServices.wtfHttpService(req).success(function(data){
					if(!data)return;
					$scope.$apply(function(){
						if(data.code == 0) {
							$scope.twolist = data.data;					
						}else {
							alert(data.msg);
						}						
					})

				}).error(function(){
					alert("渲染二级分类失败");
				});				
			}
			$scope.data.tcId = tcid;
		};

		//
		var type = 0;
		$scope.blogPost = function() {
			//判断类容
			if(!!$scope.content&&$scope.content.length > 10000) {
				alert("内容最多可输入20000个字节");
				return;
			}

			//判断标题
			if($scope.blogTitle==''){
				alert('标题不能为空');
				return;
			}else{
				if(!$scope.checkLength()){
					alert('标题最多可输入200字节');
					return;
				}
			}
			//提交表单前的标签转换请求
			if($scope.tags2==''){
				alert("请添加关键词");
			}else if($scope.tags2.length>6){
				alert("最多添加6个标签！");
			}else{
				var labelidzz=$scope.tags2.join(',');
			}

			var req = {
				type:"POST",
				uri:"addBatch",
				data:{
					"tags": labelidzz
				}
			};
			httpServices.gettabHttpService(req).success(function(data){
				if(!data)return;
					$scope.$apply(function(){
						$scope.data.labelIds = data.data;
						console.log($scope.data.labelIds);
						blogpost();
					});
			}).error(function(){
				console.log("标签请求失败");
			});



		};

		function blogpost() {
			if(!!$scope.modtype) {
				$scope.modtype = "1";
			}else {
				$scope.modtype = "0";
			}
			$scope.data.title = $(".title").val();
			$scope.data.content = $scope.content;
			$scope.data.commentopen = $('.one input[name="rad"]:checked').val();
			$scope.data.open = $('.two input[name="rad1"]:checked').val();			
			
			var radio1 = $("input[name='radio1']:checked").val();
			var radio2 = $("input[name='radio2']:checked").val();
			var radio3 = $("input[name='radio3']:checked").val();
			$scope.radiosarr = [];
			if(radio1) {
				$scope.radiosarr.push(radio1);
			}
			if(radio2) {
				$scope.radiosarr.push(radio2);
			}
			if(radio3) {
				$scope.radiosarr.push(radio3);
			}
			if($scope.radiosarr.length == 0) {
				if($scope.data.dcId) {
					var dcids = $scope.data.dcId;
				}else {
					alert("请选择详细分类");
				}
			}else {
				var dcids = $scope.radiosarr.join(",");
			}
			var req = {
				type:'POST',
				uri:'add',
				data:{
					labelIds: $scope.data.labelIds,
					title: $scope.data.title,
					tcId: $scope.data.tcId,
					scId: $scope.data.scId,
					dcIds: dcids,
					open: $scope.data.open,
					commentopen: $scope.data.commentopen,
					type: $scope.modtype,
					introduction: $scope.data.content,
					//authStatus:1,  //不传参表示博文需要审核
					seed: localStorage.seed,
					authToken:localStorage.authToken
				}
			};
			$.ajax({
				type: "post",
				dataType: "json",
				data:req.data,
				url: "http://"+allUrl+":8080/elink_user/api/blog/add",
				success: function(data) {
					if(!data)return;
					$scope.$apply(function(){
						if(data.code==0) {
							if($scope.modtype == 1){
								$("#isok").show();
								$("#qued").unbind("click");
								setTimeout(function(){
									$state.go("comIndexCtrl.resource");
								},2000)								
							}else {
								$("#isok").show();
								$("#qued").unbind("click");
								setTimeout(function(){
									$state.go("bloglist");
								},2000)								
							}
						}else {
							alert(data.msg);
						}						
					})
				},
				error: function(jqXHR) {
					alert("提交博文请求失败");
				}
			});			
		};

		//聚焦失焦动作
		$scope.titleblur = function(num) {
			if(num = 1) {
				var val = $(".title").val();
				if(val == "") {
					$("#tix").show().css("display","inline-block").html("标题不能为空");
				}
			}
		};
		$scope.titlefocus = function(num) {
			if(num==1){
				$("#tix").hide();
			}
		};

		$scope.checkLength=function(){
			var val = $scope.blogTitle;
			var len = 0;
			for(var i=0; i<val.length; i++) {
				if (val.charCodeAt(i) > 255) {
					len ++;
				}
				len ++;
			}
			if(len >=200){
				$("#tix").show().css("display","inline-block").html("最多输入200字节");
				return false;
			}else{
				$("#tix").hide();
				return true;
			}
		}
	});
});